//
//  LSFStarRateView.swift
//  Exam-iOS
//
//  Created by lishengfeng on 2018/3/9.
//  Copyright © 2018年 李胜锋. All rights reserved.
//
import UIKit


class LSFStarRateView: UIView {
    //MARK : public
    public var scorePercent: Float = 1{//得分值，范围为0--1，默认为1
        didSet{
            if self.scorePercent < 0 {
                self.scorePercent = 0
            }else if self.scorePercent > 1 {
                self.scorePercent = 1
            }
            
            newScoreHandle?(scorePercent)
            
            self.setNeedsLayout()
        }
    }
    public var hasAnimation:Bool = false  //是否允许动画，默认为false
    public var allowIncompleteStar:Bool = false //评分时是否允许不是整星，默认为NO
    public var allowUserTap: Bool = false
    public var newScoreHandle:( (Float) -> Void)?
    public func setBottomImageName(_ bottomImgName:String,topImgName:String,count:NSInteger) {
        self.bottomImageName = bottomImgName
        self.topImageName = topImgName
        self.numberOfStars = count
        buildDataAndUI()
    }
    
    //MARK : private
    private let Defalut_Star_Number = 5 //默认星星的个数
    private let Animation_Time_Tnterval = 0.2 //动画时间
    private var foregroundStarView = UIView()
    private var backgroundStarView = UIView()
    private var numberOfStars = 0
    private var bottomImageName = ""
    private var topImageName = ""
    
    private func buildDataAndUI() {
        scorePercent = 0 //默认
        hasAnimation = false
        allowIncompleteStar = false
        self.foregroundStarView = self.createStarViewWithImage(imageName: self.topImageName)
        self.backgroundStarView = self.createStarViewWithImage(imageName: self.bottomImageName)
        self.addSubview(self.backgroundStarView)
        self.addSubview(self.foregroundStarView)
        
        if allowUserTap {
            let tapGesture = UITapGestureRecognizer(target: self, action: #selector(userTapRateView(gesture:)))
            tapGesture.numberOfTapsRequired = 1
            self.addGestureRecognizer(tapGesture)
        }
    }
    
    @objc private func userTapRateView(gesture:UITapGestureRecognizer){
        let tapPoint = gesture.location(in: self)
        let offset = tapPoint.x
        let realStarScore = offset / (self.bounds.size.width / CGFloat(self.numberOfStars))
        let starScore = self.allowIncompleteStar ? Float(realStarScore) : ceilf(Float(realStarScore))
        self.scorePercent = Float(starScore / Float(self.numberOfStars))
    }
    
    private func createStarViewWithImage(imageName:String) -> UIView {
        let view = UIView(frame: self.bounds)
        view.clipsToBounds = true
        view.backgroundColor = .clear
        for index in 0..<self.numberOfStars {
            let image = UIImage.init(named: imageName)
            let imageView = UIImageView(image: image)
            
            let x = CGFloat(index) * self.bounds.size.width / CGFloat(self.numberOfStars)
            let width = self.bounds.size.width / CGFloat(self.numberOfStars)
            let height = self.bounds.size.height
                
            imageView.frame = CGRect(x: x, y: 0, width: width, height: height)
            imageView.contentMode = .scaleAspectFit
            view.addSubview(imageView)
        }
        return view
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let animationTimeInterval = self.hasAnimation ? self.Animation_Time_Tnterval : 0
        UIView.animate(withDuration: animationTimeInterval) {[weak self] in
            let width = (self?.bounds.size.width ?? 0) * CGFloat(self?.scorePercent ?? 0)
            let height = (self?.bounds.size.height ?? 0)
            self?.foregroundStarView.frame = CGRect(x: 0, y: 0, width: width, height: height)
        }
    }
    
    
    
}
